package com.fuxue.controller.user;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fuxue.dto.*;
import com.fuxue.entity.AllFiled;
import com.fuxue.entity.Cui;
import com.fuxue.result.PageResult;
import com.fuxue.result.Result;
import com.fuxue.service.QuestionService;
import com.fuxue.vo.*;
import com.fuxue.websocket.WebSocketServer;
import com.google.gson.JsonObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.lang.runtime.ObjectMethods;
import java.util.ArrayList;
import java.util.List;

@RestController("userQuestionController")
@RequestMapping("/users/user/question")
@Slf4j
@Api(tags = "用户相关接口")
public class QuestionController {
       @Autowired
       private QuestionService questionService;
       @Autowired
       private ObjectMapper objectMapper;
    /*
            得到全部领域，在server里面
        */
       @Autowired
       private WebSocketServer webSocketServer;

       @Autowired
       private RedisTemplate redisTemplate;

        /*
        写问题
         */
       @PostMapping("/writeQuestion")
       @ApiOperation("用户写问题")
       private Result write(@RequestBody WriteDTO writeDTO)
       {
           log.info("用户写问题，参数为{}",writeDTO);
              questionService.InsertQuestion(writeDTO);
              return Result.success();
       }
        /*
        首页展示问题，展示回答完的问题，而且先展示最新的，展示四个
         */
            @GetMapping("/show/{id}")
            @ApiOperation("用户首页问题展示")
            private Result<List<QuestionUserFirsetVO>> show(@PathVariable Integer id)
            {
                log.info("用户id,{}",id);
                List<QuestionUserFirsetVO> questionList = new ArrayList<QuestionUserFirsetVO>();
                questionList = questionService.getUserQuestion(id);
                return Result.success(questionList);
            }
            /*
            //__________________
            1     有一个模糊查询，select (context title)
             */
            @GetMapping("/likeselect")
            @ApiOperation("用户模糊查询")
            private Result<List<QuestionUserFirsetVO>> likeSelect(QuestonTorCDTO questonTorCDTO)
            {
            log.info("用户模糊查询的内容是,{}",questonTorCDTO);
                List<QuestionUserFirsetVO> questonlist=new ArrayList<QuestionUserFirsetVO>();
                    questonlist=questionService.likeSelect(questonTorCDTO);
            return Result.success(questonlist);
            }
       /*
       分页查询已回答问题
        */
       @PostMapping("/Finished")
       @ApiOperation("查询已完成问题")
       private Result<PageResult> UserQusetionFinishShow(@RequestBody UserQuestionPageShowDTO userQuestionPageShowDTO)
       {
           log.info("问题分页查询，参数为：{}",userQuestionPageShowDTO);
           PageResult pageResult = questionService.pageUserFinishedQuery(userQuestionPageShowDTO);
           return Result.success(pageResult);
       }

    /*
      分页查询未回答问题
     */
    @PostMapping("/notFinish")
    @ApiOperation("查询未完成问题")
    private Result<PageResult> UserQusetionUnFinishShow(@RequestBody UserQuestionPageShowDTO userQuestionPageShowDTO)
    {
        log.info("问题分页查询，参数为：{}",userQuestionPageShowDTO);
        PageResult pageResult = questionService.pageUserFinishedQuery(userQuestionPageShowDTO);
        return Result.success(pageResult);
    }
        /*
         查询未回答问题细节
         */
        @GetMapping("/UnFinisheddetials")
        @ApiOperation("展示未回答问题细节")
        private Result<QuestionDetialsVO>QaDetials(Integer id)
        {
            log.info("查询未回答问题详情，参数为：{}",id);
            QuestionDetialsVO questionDetialsVO = new QuestionDetialsVO();
            questionDetialsVO = questionService.getQuestionDetials(id);
            return Result.success(questionDetialsVO);
        }

       /*
        查询已回答回答细节
        */
        @GetMapping("/Finishdetials")
            @ApiOperation("展示已回答问题细节")
        private Result<ALLDetialsVO>FinishDetials(Integer id)
        {
            log.info("查询回答问题详情，参数为：{}",id);
            ALLDetialsVO allDetialsVO = new ALLDetialsVO();
            allDetialsVO = questionService.getALLDetials(id);
            return Result.success(allDetialsVO);
        }
        @PostMapping("/cui")
        private Result Cui(@RequestBody Cui cui) throws JsonProcessingException {

            log.info("发布催一催，参数：{}",cui);
            questionService.Cui(cui);

           // String key = cui.getQd() + "_" + System.currentTimeMillis();
           // redisTemplate.opsForValue().set(key,jsonString);
            return Result.success();
        }
        @GetMapping("/getCui")
        private Result GetCui(@PathVariable Integer id)
        {
            log.info("得到为查看的消息，参数，{}",id);
           List<Cui>list = questionService.getCui(id);


            return Result.success();
        }

}
